<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工厂" prop="factoryId">
        <el-input v-model="queryParams.factoryId" placeholder="请输入工厂" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品" prop="itemCode">
        <el-input v-model="queryParams.itemCode" placeholder="请选择产品信息" disabled >
          <el-button slot="append" icon="el-icon-search" @click="openSelectItemQuery = true"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="产品类型" prop="itemType">
        <el-select v-model="queryParams.itemType" placeholder="请选择产品类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.WMS_ITEM_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['wms:item-warehouse:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['wms:item-warehouse:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="工厂" align="center" prop="factoryName" />
      <el-table-column label="产品名称" align="center" prop="itemName" width="100px" />
      <el-table-column label="产品代码" align="center" prop="itemCode" width="100px" />
      <el-table-column label="产品规格" align="center" prop="itemSpec" width="100px" show-overflow-tooltip />
      <el-table-column label="产品图号" align="center" prop="drawCode" width="100px"/>
      <el-table-column label="产品类型" align="center" prop="itemType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.WMS_ITEM_TYPE" :value="scope.row.itemType" />
        </template>
      </el-table-column>
      <el-table-column label="出库策略" align="center" prop="strategy">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.WMS_STRATEGY" :value="scope.row.strategy" />
        </template>
      </el-table-column>
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="默认仓库" align="center" prop="warehouseName" width="100px" show-overflow-tooltip />
      <el-table-column label="默认库区" align="center" prop="areaName" width="100px" show-overflow-tooltip />
      <el-table-column label="默认库位" align="center" prop="locationName" width="100px" show-overflow-tooltip />
      <el-table-column label="标准库存" align="center" prop="standardStock" />
      <el-table-column label="安全库存" align="center" prop="salfStock" />
      <el-table-column label="补货点" align="center" prop="reorderPoint" />
      <el-table-column label="首次入库日" align="center" prop="firstInTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.firstInTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后入库日" align="center" prop="lastInTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.lastInTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后出库日" align="center" prop="lastOutTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.lastOutTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100px" fixed="right">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['wms:item-warehouse:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['wms:item-warehouse:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工厂" prop="factoryId">
              <SelectFactory v-model="form.factoryId"></SelectFactory>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请选择产品信息" disabled >
                <el-button slot="append" icon="el-icon-search" @click="openSelectItem = true"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品代码" prop="itemCode">
              <el-input v-model="form.itemCode" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品规格" prop="itemSpec">
              <el-input v-model="form.itemSpec" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品图号" prop="drawCode">
              <el-input v-model="form.drawCode" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品类型" prop="itemType">
              <el-select v-model="form.itemType" placeholder="请选择产品类型">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.WMS_ITEM_TYPE)"
                           :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库策略" prop="strategy">
              <el-select v-model="form.strategy" placeholder="请选择出库策略">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.WMS_STRATEGY)"
                           :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="默认仓库" prop="warehouseArr">
              <selectWarehouse v-model="form.warehouseArr" v-if="editCascaderVisible"></selectWarehouse>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标准库存" prop="standardStock">
              <el-input type="number" v-model="form.standardStock" placeholder="请输入标准库存" />
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="安全库存" prop="salfStock">
              <el-input type="number" v-model="form.salfStock" placeholder="请输入安全库存" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="补货点" prop="reorderPoint">
              <el-input type="number" v-model="form.reorderPoint" placeholder="请输入补货点" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                          :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :autosize="{minRows:2, maxRows:6}" v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查询产品信息（查询使用）   -->
    <SelectItem :openSelectItem.sync="openSelectItemQuery" @selectItem="selectItemQuery"></SelectItem>
    <!-- 查询产品信息（表单使用）   -->
    <SelectItem :openSelectItem.sync="openSelectItem" @selectItem="selectItem"></SelectItem>
  </div>
</template>

<script>
import { createItemWarehouse, updateItemWarehouse, deleteItemWarehouse, getItemWarehouse, getItemWarehousePage, exportItemWarehouseExcel } from "@/api/wms/itemWarehouse";
import SelectFactory from '@/components/SelectFactory/index.vue'
import SelectItem from '@/components/SelectItem/index.vue'
import SelectWarehouse from '@/components/SelectWarehouse/index.vue'

export default {
  name: "ItemWarehouse",
  components: {
    SelectFactory,
    SelectItem,
    SelectWarehouse
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品仓储信息列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示产品弹窗
      openSelectItemQuery: false,
      openSelectItem: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        factoryId: null,
        itemId: null,
        itemType: null,
        status: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 库区位数组
      warehouseArr: [],
      editCascaderVisible : true,
      // 表单校验
      rules: {
        factoryId: [{ required: true, message: "工厂不能为空", trigger: "blur" }],
        itemId: [{ required: true, message: "产品不能为空", trigger: "blur" }],
        itemName: [{ required: true, message: "产品不能为空", trigger: "blur" }],
        itemType: [{ required: true, message: "产品类型不能为空", trigger: "change" }],
        strategy: [{ required: true, message: "出库策略不能为空", trigger: "change" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getItemWarehousePage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /* 选择产品回调 */
    selectItemQuery(data) {
      this.queryParams.itemId = data.id;
      this.queryParams.itemCode = data.code;
    },
    selectItem(data) {
      this.form.itemId = data.id;
      this.form.itemName = data.name;
      this.form.itemCode = data.code;
      this.form.itemSpec = data.spec;
      this.form.drawCode = data.drawCode;
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        factoryId: undefined,
        itemId: undefined,
        itemName: undefined,
        warehouseId: undefined,
        areaId: undefined,
        locationId: undefined,
        warehouseArr: [],
        itemType: undefined,
        strategy: undefined,
        standardStock: undefined,
        salfStock: undefined,
        reorderPoint: undefined,
        remark: undefined,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品仓储信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getItemWarehouse(id).then(response => {
        this.form = response.data;
        //处理仓库数据
        this.form.warehouseArr = [];
        if (this.form.warehouseId != null && this.form.warehouseId != undefined && this.form.warehouseId != 0) {
          this.form.warehouseArr.push("0-" + this.form.warehouseId);
        }
        if (this.form.areaId != null && this.form.areaId != undefined&& this.form.areaId != 0) {
          this.form.warehouseArr.push("1-" + this.form.areaId);
        }
        if (this.form.locationId != null && this.form.locationId != undefined&& this.form.locationId != 0) {
          this.form.warehouseArr.push("2-" + this.form.locationId);
        }
        this.editCascaderVisible = false;
        this.$nextTick(() => {
          this.editCascaderVisible = true;
        })
        this.open = true;
        this.title = "修改产品仓储信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 处理仓库
        this.form.warehouseId = 0;
        this.form.areaId = 0;
        this.form.locationId = 0;
        if (this.form.warehouseArr[0] != null && this.form.warehouseArr[0] != undefined) {
          this.form.warehouseId =  this.form.warehouseArr[0].split("-")[1];
        }
        if (this.form.warehouseArr[1]!= null && this.form.warehouseArr[1] != undefined) {
          this.form.areaId =  this.form.warehouseArr[1].split("-")[1];
        }
        if (this.form.warehouseArr[2] != null && this.form.warehouseArr[2] != undefined) {
          this.form.locationId =  this.form.warehouseArr[2].split("-")[1];
        }
        // 修改的提交
        if (this.form.id != null) {
          updateItemWarehouse(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createItemWarehouse(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除产品仓储信息编号为"' + id + '"的数据项?').then(function() {
          return deleteItemWarehouse(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有产品仓储信息数据项?').then(() => {
          this.exportLoading = true;
          return exportItemWarehouseExcel(params);
        }).then(response => {
          this.$download.excel(response, '产品仓储信息.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
